package com.lnsf.bookstore.system.category.service.impl;

import com.lnsf.bookstore.system.category.dao.CategoryMapper;
import com.lnsf.bookstore.system.category.service.CategoryService;
import com.lnsf.bookstore.system.category.vo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService{

    @Autowired
    private CategoryMapper categoryMapper;

    /* @Override
     public List<ApiCategory> selectAll() {
         List<ApiCategory> categoryList = categoryMapper.selectAll();
         if(categoryList==null){
             return  null;
         }
         Map<String,List<ApiCategory>> mapChildren = new HashMap<>();
         for (ApiCategory category : categoryList){
             String parentCode = category.getParentCode();
             if(parentCode.equals("0")){
                 parentCode = "";
             }
             List<ApiCategory> children = mapChildren.get(parentCode);  // 通过parentCode能找到children不为空，那就不new
             if(children == null||children.size()==0){ //主要是第一层的时候，需要new一个新的
                 children = new ArrayList<>();
             }
             children.add(category);
             mapChildren.put(parentCode,children);
         }
         for (ApiCategory category:categoryList){
             category.setChildren(mapChildren.get(category.getCode()));
         }
         return mapChildren.get("");//返回第一层

     }*/
    @Override
    public List<Category> selectAll() {
            return categoryMapper.selectAll();
    }

    @Override
    public List<Category> selectParent(Category category) {
        return categoryMapper.selectByObject(category);
    }

    /**
     * 根据主键查询实体
     *
     * @param id 主键
     * @return 实体
     */
    public Category queryObject(Integer id){
        return categoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public int delete(Integer id) {
        return categoryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int save(Category category) {
        return categoryMapper.insert(category);
    }

    @Override
    public int update(Category category) {
        return categoryMapper.updateByPrimaryKey(category);
    }

    @Override
    public int deleteBatch(Integer[] ids) {
        return categoryMapper.deleteBatch(ids);
    }
}
